package com.tarim.wheat.controller;

import com.tarim.wheat.model.User;
import com.tarim.wheat.service.UserService;
import com.tarim.wheat.util.Constants;
import com.tarim.wheat.util.JsonResult;
import com.tarim.wheat.vo.Criteria;
import com.tarim.wheat.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/findUserName")
    @ResponseBody
    public JsonResult findUserName(User user){
        Long count = userService.selectByUserName(user);
        if(count == 0){
            return new JsonResult(Constants.STATUS_SUCCESS, "该用户名可以使用", count);
        }
        return new JsonResult(Constants.STATUS_FAIL, "该用户名已被注册", count);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult insert(User user){
        JsonResult js;
        try {
            int i = userService.insert(user);
            if(i == 0){
                js = new JsonResult(Constants.STATUS_FAIL, "注册失败，用户名已存在");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "注册成功");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "注册异常");
        }
        return js;
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult login(User user){
        JsonResult js;
        try {
            User record = userService.selectForLogin(user);
            if(record == null){
                js = new JsonResult(Constants.STATUS_NOT_FOUND, "用户名或密码错误");
                return js;
            }
            js = new JsonResult(Constants.STATUS_SUCCESS, "登录成功", record);
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "登录异常");
        }
        return js;
    }

    /**
     * 获取用户（分页）
     * @param pageIndex
     * @param pageSize
     * @param userName
     * @return
     */
    @RequestMapping(value = "/getUsers")
    @ResponseBody
    public JsonResult getUsers(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "userName", defaultValue = "") String userName){
        JsonResult js;
        try {
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("userName", userName);
            pageBean = userService.selectByCondition(criteria, pageBean);
            js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return js;
    }

    /**
     * 恢复/删除
     * @param user
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult update(User user){
        JsonResult js;
        try {
            int i = userService.updateDr(user);
            if(i == 0){
                js = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }

}
